//
//  ViewController.m
//  label使用富文本加载HTML文件
//
//  Created by 李煜 on 2021/9/15.
//
//链接：https://www.jianshu.com/p/ade99cf9f7c8

#import "ViewController.h"
#import "hpple/TFHpple.h"
@interface ViewController ()
@property (nonatomic,strong) UILabel *label;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    NSString *path = [[NSBundle mainBundle]pathForResource:@"附件展示.html" ofType:nil];
    NSData *data = [NSData dataWithContentsOfFile:path];
    
    NSAttributedString * attrStr = [[NSAttributedString alloc] initWithData:data options:@{ NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType } documentAttributes:nil error:nil];
    UILabel *lb = [[UILabel alloc]initWithFrame:CGRectMake(0, 40, 375, 627)];
    lb.numberOfLines = 0;
    self.label = lb;
    [self.view addSubview:lb];
    [self.label setAttributedText:attrStr];
    [lb sizeToFit];
    

    TFHpple * doc       = [[TFHpple alloc] initWithHTMLData:data];
    NSArray * elements  = [doc searchWithXPathQuery:@"//div[@class='thumbs']"];

    TFHppleElement * e = [elements objectAtIndex:0];
    
    [e text];                       // The text inside the HTML element (the content of the first text node)
    [e tagName];                    // "a"
    [e attributes];                 // NSDictionary of href, class, id, etc.
    [e objectForKey:@"href"];       // Easy access to single attribute
    [e firstChildWithTagName:@"b"]; // The first "b" child n
    
}


@end
